﻿using System;
using System.Collections.Generic;
using System.Text;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using FireStarterModeller.UI;
using FireStarterModeller.UI.ViewEditor;
using Habanero.Base;
using Habanero.BO.ClassDefinition;
using Habanero.Faces.Base;
using NUnit.Framework;
using Rhino.Mocks;

namespace FireStarterModeller.Test.UI.ViewEditor
{
    [TestFixture]
    public class TestUIXmlEditorControl
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }

        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();
        }

        private static IControlFactory GetControlFactory()
        {
            return GlobalUIRegistry.ControlFactory;
        }

        [Test]
        public void Test_CurrentUIView_Null()
        {
            //---------------Set up test pack-------------------
            DMSolution dmSolution = TestUtilsShared.CreateSavedDMSolution();
            DMClass dmClass = dmSolution.DefaultAssembly.CreateClass(TestUtilsShared.GetRandomString());
            UIXmlEditorControl control = CreateUIXmlEditorControl();

            //---------------Execute Test ----------------------
            control.CurrentUIView = null;

            //---------------Test Result -----------------------
            StringAssert.Contains("<ui></ui>", control.UIViewXmlEditor.Text);
            //---------------Tear Down -------------------------          
        }

        [Test]
        public void Test_CurrentUIView()
        {
            //---------------Set up test pack-------------------
            DMSolution dmSolution = TestUtilsShared.CreateSavedDMSolution();
            DMClass dmClass = dmSolution.DefaultAssembly.CreateClass(TestUtilsShared.GetRandomString());
            UIView uiView = dmClass.UIViews.CreateBusinessObject();
            uiView.ViewName = TestUtilsShared.GetRandomString();
            UIXmlEditorControl control = CreateUIXmlEditorControl();

            //---------------Execute Test ----------------------
            control.CurrentUIView = uiView;

            //---------------Test Result -----------------------
            Assert.AreSame(uiView, control.CurrentUIView);
            StringAssert.Contains(string.Format(@"<ui name=""{0}"">", uiView.ViewName), control.UIViewXmlEditor.Text);
            StringAssert.Contains("</ui>", control.UIViewXmlEditor.Text);
            //---------------Tear Down -------------------------          
        }

        //[Test]
        //public void Test_SetUIViews_TwoUIViews()
        //{
        //    //---------------Set up test pack-------------------
        //    DMSolution dmSolution = TestUtilsShared.CreateSavedDMSolution();
        //    DMClass dmClass = dmSolution.DefaultAssembly.CreateClass(TestUtilsShared.GetRandomString());
        //    UIView uiView1 = dmClass.UIViews.CreateBusinessObject();
        //    uiView1.ViewName = TestUtilsShared.GetRandomString();
        //    UIView uiView2 = dmClass.UIViews.CreateBusinessObject();
        //    uiView2.ViewName = TestUtilsShared.GetRandomString();
        //    UIXmlEditorControl control = CreateUIXmlEditorControl();

        //    //---------------Execute Test ----------------------
        //    control.UIViews = dmClass.UIViews;

        //    //---------------Test Result -----------------------
        //    Assert.AreEqual(2, control.UIViewSelectorControl.UIViews.Count);
        //    Assert.AreSame(uiView1, control.UIViewSelectorControl.SelectedUIView);
        //    StringAssert.Contains(string.Format(@"<ui name=""{0}"">", uiView1.ViewName), control.UIViewXmlEditor.Text);
        //    Assert.IsFalse(control.UIViewXmlEditor.Text.Contains(string.Format(@"<ui name=""{0}"">", uiView2.ViewName)));
        //    //---------------Tear Down -------------------------          
        //}
        
        [Test]
        public void TestSelectedUIGrid()
        {
            //---------------Set up test pack-------------------
            UIXmlEditorControl editorControl = CreateUIXmlEditorControl();
            DMSolution dmSolution = TestUtilsShared.CreateSavedDMSolution();
            DMClass dmClass = dmSolution.DefaultAssembly.CreateClass(TestUtilsShared.GetRandomString());
            UIView uiView1 = dmClass.UIViews.CreateBusinessObject();
            uiView1.ViewName = TestUtilsShared.GetRandomString();
            editorControl.CurrentUIView = uiView1;
            //---------------Execute Test ----------------------
            IUIDef uiDef = editorControl.CurrentUIDef;
            //---------------Test Result -----------------------
            Assert.IsNotNull(uiDef);
            Assert.AreEqual(uiView1.ViewName, uiDef.Name);
            //---------------Tear Down -------------------------          
        }

        private UIXmlEditorControl CreateUIXmlEditorControl()
        {
            return new UIXmlEditorControl(GetControlFactory(), new DMToClassDefsXmlConverter(), GetControlFactory().CreateTextBox());
        }
    }
}
